home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10976 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.6 KB  |  51 lines

  1. Path: lade.news.pipex.net!pipex!dircon!usenet
  2. From: topher@dircon.co.uk (Chris Pyman)
  3. Newsgroups: comp.lang.c++
  4. Subject: Memory Leak?
  5. Date: Tue, 12 Mar 1996 12:16:58 GMT
  6. Organization: Direct Connection
  7. Message-ID: <4i2tr9$q6k@newsgate.dircon.co.uk>
  8. NNTP-Posting-Host: gw2-157.pool.dircon.co.uk
  9. X-Newsreader: Forte Free Agent 1.0.82
  10.  
  11. Hi all
  12.  
  13. Does anybody know whether the following would lead to a memory leak?
  14. (Assume this is a Win32 Console App written with Visual C++ v2.0)
  15.  
  16.  
  17. main(int argc, char* argv[]) {
  18.     CString s1("31-12-99");
  19.     CString s2;
  20.  
  21.     s2 = s1.Mid(0,2) + s1.Mid(3,2) + s1.Mid(6,2);
  22.  
  23.     cout << s2;
  24.     return 0;
  25. }
  26.  
  27.  
  28. Obviously the idea is to set s2 to "311299", but I would have thought
  29. that the Mid() method creates a new CString object and returns a
  30. reference to it, in which case what becomes of the three "temporary"
  31. objects used to build the string in s2?  Are they just floating around
  32. with no way to get at them, or do they get properly deleted?  And if
  33. the latter, how does MFC manage it?
  34.  
  35. The main reason I'm asking this is because I want to implement a
  36. string class of my own, and would like to be able to use it to build
  37. strings as simply as shown above, but I don't want loads of stray
  38. objects floating around.  
  39. Oh, and before you ask, no I can't just use MFC because my programs
  40. have to run on OS/2 as well as Win32.  So I have to stick to
  41. standard-library stuff, and any classes I use have to be my own.
  42.  
  43. Looking forward to hearing from you
  44. Sorry if I'm being ignorant or dimwitted, but I've only read as much
  45. of Stroustrup's book as my puny brain can take in at the moment.
  46.  
  47. l+k
  48. chris
  49.  
  50.  
  51.